package org.example.design.observer.event.service;

import org.example.design.observer.bean.LotteryResult;
import org.example.design.observer.event.EventManager;
import org.example.design.observer.event.listener.MQEventListener;
import org.example.design.observer.event.listener.MessageEventListener;

/**
 * @Classname LotteryService
 * @Description 摇号业务抽象类
 * @Date 2020/11/11 10:43 上午
 * @Created by xiangjd
 */
public abstract class LotteryService {

    private EventManager eventManager;

    public LotteryService() {
        eventManager = new EventManager(EventManager.EventType.MQ, EventManager.EventType.Message);
        eventManager.subscribe(EventManager.EventType.MQ, new MQEventListener());
        eventManager.subscribe(EventManager.EventType.Message, new MessageEventListener());
    }

    public LotteryResult draw(String uid){
        LotteryResult lotteryResult = doDraw(uid);
        //需要什么通知就调用什么方法
        eventManager.notify(EventManager.EventType.MQ,lotteryResult);
        eventManager.notify(EventManager.EventType.Message,lotteryResult);
        return lotteryResult;
    }

    /**
     * 基础业务方法（摇号）
     * @param uid
     * @return
     */
    protected abstract LotteryResult doDraw(String uid);

}
